home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 726-750 / 741 / rkrm_lib1 / rkrm_lib1.lha / Intuition / IO_Methods / eventloop.c < prev   
C/C++ Source or Header  |  1992-09-03  |  7KB  |  218 lines

  1. ;/* eventloop.c - Execute me to compile me with SAS C 5.10
  2. LC -b1 -cfistq -v -y -j73 eventloop.c
  3. Blink FROM LIB:c.o,eventloop.o TO eventloop LIBRARY LIB:LC.lib,LIB:Amiga.lib
  4. quit
  5. */
  6.  
  7. /*
  8. Copyright (c) 1992 Commodore-Amiga, Inc.
  9.  
  10. This example is provided in electronic form by Commodore-Amiga, Inc. for
  11. use with the "Amiga ROM Kernel Reference Manual: Libraries", 3rd Edition,
  12. published by Addison-Wesley (ISBN 0-201-56774-1).
  13.  
  14. The "Amiga ROM Kernel Reference Manual: Libraries" contains additional
  15. information on the correct usage of the techniques and operating system
  16. functions presented in these examples.  The source and executable code
  17. of these examples may only be distributed in free electronic form, via
  18. bulletin board or as part of a fully non-commercial and freely
  19. redistributable diskette.  Both the source and executable code (including
  20. comments) must be included, without modification, in any copy.  This
  21. example may not be published in printed form or distributed with any
  22. commercial product.  However, the programming techniques and support
  23. routines set forth in these examples may be used in the development
  24. of original executable software products for Commodore Amiga computers.
  25.  
  26. All other rights reserved.
  27.  
  28. This example is provided "as-is" and is subject to change; no
  29. warranties are made.  All use is at your own risk. No liability or
  30. responsibility is assumed.
  31. */
  32.  
  33.  
  34. /*
  35. ** eventloop.c - standard technique to handle IntuiMessages from an IDCMP.
  36. */
  37. #define INTUI_V36_NAMES_ONLY
  38.  
  39. #include <exec/types.h>
  40. #include <intuition/intuition.h>
  41.  
  42. #include <clib/exec_protos.h>
  43. #include <clib/intuition_protos.h>
  44.  
  45. #include <stdio.h>
  46.  
  47. #ifdef LATTICE
  48. int CXBRK(void)    { return(0); }  /* Disable Lattice CTRL/C handling */
  49. int chkabort(void) { return(0); }  /* really */
  50. #endif
  51.  
  52. /* our function prototypes */
  53. BOOL handleIDCMP(struct Window *win, BOOL done);
  54.  
  55. struct Library *IntuitionBase = NULL;
  56.  
  57.  
  58. /*
  59. ** main routine.
  60. ** Open required library and window, then process the events from the
  61. ** window.  Free all resources when done.
  62. */
  63. VOID main(int argc, char **argv)
  64. {
  65. ULONG signals;
  66. UBYTE done;
  67. struct Window *win;
  68.  
  69. IntuitionBase = (struct IntuitionBase *)OpenLibrary("intuition.library",37);
  70. if (IntuitionBase != NULL)
  71.     {
  72.     if (win = OpenWindowTags(NULL,
  73.                         WA_Title,       "Press Keys and Mouse in this Window",
  74.                         WA_Width,       500,
  75.                         WA_Height,      50,
  76.                         WA_Activate,    TRUE,
  77.                         WA_CloseGadget, TRUE,
  78.                         WA_RMBTrap,     TRUE,
  79.                         WA_IDCMP, IDCMP_CLOSEWINDOW | IDCMP_VANILLAKEY |
  80.                             IDCMP_RAWKEY | IDCMP_DISKINSERTED |
  81.                             IDCMP_DISKREMOVED | IDCMP_MOUSEBUTTONS,
  82.                         TAG_END))
  83.         {
  84.         done = FALSE;
  85.  
  86.         /* perform this loop until the message handling routine signals
  87.         ** that we are done.
  88.         **
  89.         ** When the Wait() returns, check which signal hit and process
  90.         ** the correct port.  There is only one port here, so the test
  91.         ** could be eliminated.  If multiple ports were being watched,
  92.         ** the test would become:
  93.         **
  94.         **    signals = Wait( (1L << win1->UserPort->mp_SigBit) |
  95.         **                    (1L << win2->UserPort->mp_SigBit) |
  96.         **                    (1L << win3->UserPort->mp_SigBit))
  97.         **    if (signals & (1L << win1->UserPort->mp_SigBit))
  98.         **        done = handleWin1IDCMP(win1,done);
  99.         **    else if (signals & (1L << win2->UserPort->mp_SigBit))
  100.         **        done = handleWin2IDCMP(win2,done);
  101.         **    else if (signals & (1L << win3->UserPort->mp_SigBit))
  102.         **        done = handleWin3IDCMP(win3,done);
  103.         **
  104.         ** Note that these could all call the same routine with different
  105.         ** window pointers (if the handling was identical).
  106.         **
  107.         ** handleIDCMP() should remove all of the messages from the port.
  108.         */
  109.         while (!done)
  110.             {
  111.             signals = Wait(1L << win->UserPort->mp_SigBit);
  112.             if (signals & (1L << win->UserPort->mp_SigBit))
  113.                 done = handleIDCMP(win,done);
  114.             };
  115.         CloseWindow(win);
  116.         }
  117.     CloseLibrary(IntuitionBase);
  118.     }
  119. }
  120.  
  121.  
  122. /*
  123. ** handleIDCMP() - handle all of the messages from an IDCMP.
  124. */
  125. BOOL handleIDCMP(struct Window *win, BOOL done)
  126. {
  127. struct IntuiMessage *message;
  128. USHORT code;
  129. SHORT mousex, mousey;
  130. ULONG class;
  131.  
  132. /* Remove all of the messages from the port by calling GetMsg()
  133. ** until it returns NULL.
  134. **
  135. ** The code should be able to handle three cases:
  136. **
  137. ** 1.  No messages waiting at the port, and the first call to GetMsg()
  138. ** returns NULL.  In this case the code should do nothing.
  139. **
  140. ** 2.  A single message waiting.  The code should remove the message,
  141. ** processes it, and finish.
  142. **
  143. ** 3.  Multiple messages waiting.  The code should process each waiting
  144. ** message, and finish.
  145. */
  146. while (NULL != (message = (struct IntuiMessage *)GetMsg(win->UserPort)))
  147.     {
  148.     /* It is often convenient to copy the data out of the message.
  149.     ** In many cases, this lets the application reply to the message
  150.     ** quickly.  Copying the data is not required, if the code does
  151.     ** not reply to the message until the end of the loop, then
  152.     ** it may directly reference the message information anywhere
  153.     ** before the reply.
  154.     */
  155.     class  = message->Class;
  156.     code   = message->Code;
  157.     mousex = message->MouseX;
  158.     mousey = message->MouseY;
  159.  
  160.     /* The loop should reply as soon as possible.  Note that the code
  161.     ** may not reference data in the message after replying to the
  162.     ** message.  Thus, the application should not reply to the message
  163.     ** until it is done referencing information in it.
  164.     **
  165.     ** Be sure to reply to every message received with GetMsg().
  166.     */
  167.     ReplyMsg((struct Message *)message);
  168.  
  169.     /* The class contains the IDCMP type of the message. */
  170.     switch (class)
  171.         {
  172.         case IDCMP_CLOSEWINDOW:
  173.             done = TRUE;
  174.             break;
  175.         case IDCMP_VANILLAKEY:
  176.             printf("IDCMP_VANILLAKEY (%lc)\n",code);
  177.             break;
  178.         case IDCMP_RAWKEY:
  179.             printf("IDCMP_RAWKEY\n");
  180.             break;
  181.         case IDCMP_DISKINSERTED:
  182.             printf("IDCMP_DISKINSERTED\n");
  183.             break;
  184.         case IDCMP_DISKREMOVED:
  185.             printf("IDCMP_DISKREMOVED\n");
  186.             break;
  187.         case IDCMP_MOUSEBUTTONS:
  188.             /* the code often contains useful data, such as the ASCII
  189.             ** value (for IDCMP_VANILLAKEY), or the type of button
  190.             ** event here.
  191.             */
  192.             switch (code)
  193.                 {
  194.                 case SELECTUP:
  195.                     printf("SELECTUP at %d,%d\n",mousex,mousey);
  196.                     break;
  197.                 case SELECTDOWN:
  198.                     printf("SELECTDOWN at %d,%d\n",mousex,mousey);
  199.                     break;
  200.                 case MENUUP:
  201.                     printf("MENUUP\n");
  202.                     break;
  203.                 case MENUDOWN:
  204.                     printf("MENUDOWN\n");
  205.                     break;
  206.                 default:
  207.                     printf("UNKNOWN CODE\n");
  208.                     break;
  209.                 }
  210.             break;
  211.         default:
  212.             printf("Unknown IDCMP message\n");
  213.             break;
  214.         }
  215.     }
  216. return(done);
  217. }
  218.